package com.businessportal.web.notifications.email;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;

import javax.ws.rs.core.MediaType;

/**
 * Created by Vadim on 7/8/14.
 *
 * www.mailgun.com
 * login: str.vadim@gmail.com
 * pass: bullshit128
 */
public class MailGunClient {
    private static final String api = "api";
    private static final String key = "key-0t4-omjkxiffhhhdi3j54t-328mkth96";
    private static final String domain = "sandboxe973b69e6c574d938b6111829150813b.mailgun.org";

    private String from, to, subject, body;

    public MailGunClient(String to, String subject, String body){
        from = "News Letter <abc@" + domain + ">";
        this.to = to;
        this.subject = subject;
        this.body = body;
    }


    public void Execute(){
        try{

            Client client = Client.create();
            client.addFilter( new HTTPBasicAuthFilter(api, key));

            WebResource webResource = client.resource("https://api.mailgun.net/v2/" + domain + "/messages");

            MultivaluedMapImpl formData = new MultivaluedMapImpl();
            formData.add("from", from);
            formData.add("to", to);
            formData.add("subject", subject);
            formData.add("html", body);

            ClientResponse clientResponse = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);

            String out = clientResponse.getEntity(String.class);

            System.out.println(out);

        } catch (Exception e){
            e.printStackTrace();
        }
    }

}
